//-----------------------------------------------------------------------------
// File: xaux.h
//
// Desc: auxiliary utility.
//       build20070120
//
// Author: Silva Lam
//-----------------------------------------------------------------------------
#ifndef _XAUX_H_
#define _XAUX_H_
#include "xtypes.h"
#include "xmemory.h"
#include "xstring.h"


#pragma warning(disable:4305)
#pragma warning(disable:4244)
#pragma warning(disable:4786)

/*-----------------------------------------------------------------
Macro
-----------------------------------------------------------------*/
#define AUX_MAKEWORD(a, b)      ((WORD)(((BYTE)(a)) | ((WORD)((BYTE)(b))) << 8))
#define AUX_MAKELONG(a, b)      ((LONG)(((WORD)(a)) | ((DWORD)((WORD)(b))) << 16))
#define AUX_LOWORD(l)           ((WORD)(l))
#define AUX_HIWORD(l)           ((WORD)(((DWORD)(l) >> 16) & 0xFFFF))
#define AUX_LOBYTE(w)           ((BYTE)(w))
#define AUX_HIBYTE(w)           ((BYTE)(((WORD)(w) >> 8) & 0xFF))

#define AUX_MAKEDWORD(a, b, c, d)	(((DWORD)AUX_MAKEWORD((a),(b))) | ((DWORD)AUX_MAKEWORD(c,(d)) <<16))
#define AUX_LODWORD(qw)				((DWORD)(qw))
#define AUX_HIDWORD(qw)				((DWORD)((UINT64)(qw) >> 32 & 0xFFFFFFFF))

//math macro
#define AUX_ROUND(x)			((INT)((x)+0.5))
#define AUX_INTEGER(x)			((INT)(x))
#define AUX_MAX(a,b)            (((a) > (b)) ? (a) : (b))
#define AUX_MIN(a,b)            (((a) < (b)) ? (a) : (b))
#define AUX_ABS(x)				(((x) < 0) ? -(x) : (((x) > 0) ? (x) : 0))
#define AUX_SGN(x)				(((x) < 0) ? -1 : (((x) > 0) ? 1 : 0))

#define AUX_MAX3(a,b,c)            (((((a) > (b)) ? (a) : (b)) > (c)) ? ((((a) > (b)) ? (a) : (b))) : (c))
#define AUX_MIN3(a,b,c)            (((((a) < (b)) ? (a) : (b)) < (c)) ? ((((a) < (b)) ? (a) : (b))) : (c))
#define AUX_MID3(a,b,c)            ()

//useful macro
#define AUX_FIX_SUBRANGE_VALID(x,a,b) \
	(((x) < AUX_MIN((a),(b))) ? AUX_MIN((a),(b)) : (((x) > AUX_MAX((a),(b))) ? AUX_MAX((a),(b)) : (x)))

#define AUX_IS_SUBRANGE_VALID(x,a,b) \
	(((x)-(a))*((x)-(b))>0)

#define AUX_ZERO_STRUCT(_Arg) xMemorySet(&_Arg, 0, sizeof(_Arg))
#define AUX_SAFE_RELEASE(pObj) if(pObj != NULL){pObj->Release();pObj = NULL;};(0)
#define AUX_ZERO_MEMORY(Destination,Length) xMemorySet((Destination),0,(Length))

/*-----------------------------------------------------------------
Classes
-----------------------------------------------------------------*/
template <class _Ty>
class TxSingletonClass
{
public:
	static _Ty * GetInstance()
	{
		static _Ty _Ref;
		return &_Ref;
	}

	static _Ty & Instance()
	{
		return (*GetInstance());
	}
};




/*-----------------------------------------------------------------
Functions
-----------------------------------------------------------------*/
template<class _Ty>
inline void xSwapVar(_Ty &l, _Ty &r)
{
	_Ty tmp;
	tmp = r;
	r = l;
	l = tmp;
}



ulong_t xGetSecondsFrom1970();
ulong_t xGetTickCount();


bool_t xIsLittleEndian();
long_t xGetFileLength(lpcstr_t lpszPathName);

void xLogConsole( lpcstr_t szInfo );
void xLogFile( lpcstr_t szInfo, lpcstr_t szFileName="debug.log" );

bool_t xIsFileExist( lpcstr_t lpszPathName );
void xLoadMemoryBlock( memptr_t _DataPtr, int_t _Size, lpcstr_t lpszPathName );
void xSaveMemoryBlock( memptr_t _DataPtr, int_t _Size, lpcstr_t lpszPathName );

qword_t xGetCycleCount();
dword_t xBGRA2RGBA(dword_t _Color);

qword_t xEndianSwitchU64( qword_t _QW );
dword_t xEndianSwitchU32( dword_t _DW );
word_t xEndianSwitchU16( word_t _W );




/*-------------------------------------------------------------
Functions
-------------------------------------------------------------*/
int_t xConvertStringToInteger(const CxString& _Str );
CxString xTrimString( const CxString& _Str );


CxString xConvertStringToUpperCase( const CxString& _Str );
CxString xConvertStringToLowerCase( const CxString& _Str );
CxString xStringCapitalize( const CxString& _Str );
CxString xStringInvertCase( const CxString& _Str );





#endif //_XAUX_H_
